import {Component} from '@angular/core';
import {NavController, NavParams} from 'ionic-angular';

// import particlesJS from 'particles';

import {Backend} from '../../providers/backend';

import {Information} from './tab-my-information';
import {Changepaw} from './tab-my-changepaw';
import {Contact} from './tab-my-contact';
import {About} from './tab-my-about';
import {Conference} from './tab-my-conference';
import {LoginPage} from './tab-my-login';
import { Events } from 'ionic-angular';

@Component({
  templateUrl: 'build/pages/tab-my/tab-my.html'
})
export class TabMy {
  static get parameters() {
    return [[NavController], [NavParams], [Backend], [Events]];
  }
  constructor(nav, params, backend, events) {
    this.nav = nav;
    this.params = params;
    this.backend = backend;
    this.uid = params.get('id');
    this.email = params.get('email');
    this.user = {};
    this.contents;
    this.username;
    this.events = events;
    this.img1 = 'asset/icon/me1.png';
    this.img2 = 'asset/icon/me2.png';
    this.img3 = 'asset/icon/me3.png';
    this.img4 = 'asset/icon/me4.png';
    this.img5 = 'asset/icon/me5.png';

    this.events.subscribe('user:created', (userEventData) => {
      // userEventData is an array of parameters, so grab our first and only arg
      console.log(userEventData[0]);
      this.username = userEventData[0];
    });
    // if(typeof (this.uid) === 'string'){
    //   this.backend.loginsuccess(this.uid)
    //   .then(result => {
    //     this.contents = result.result[0];
    //     console.log(this.contents);
    //   });
    // }
    this.backend.isLogined()
      .then(result => {
        console.log(result);
        this.result = result;
        this.uid = this.result.uid;
        this.email = this.result.email;

        if (result.didTutorial == 'true') {
          this.backend.loginsuccess()
            .then(result => {
              this.contents = result.result[0];
              console.log(this.contents);
              this.username=result.result[0].lname;

            });
        } else {
          this.nav.rootNav.push(LoginPage, { 'email': this.result.email, 'uid': this.result.uid });
        }
      });

  }


  ionViewWillEnter() {
    console.log('ionViewWillEnter');

    console.log(window.particlesJS);

    window.particlesJS.load('banner-particles', 'asset/particles.json', function () {
      console.log('callback - particles.js config loaded');
    });
  }

  toConference() {
    this.backend.isLogined()
      .then(result => {
        this.result = result;


        if (this.result.didTutorial == 'true') {
          this.uid = this.result.uid;
          this.email = this.result.email;
          this.nav.push(Conference, { contents: this.contents, email: this.email, uid: this.uid });
        }
      });

  }
  toInformation() {
    this.nav.push(Information, { contents: this.contents, email: this.email, uid: this.uid });
  }
  toChangepaw() {
    this.nav.push(Changepaw, { uid: this.uid });
  }
  toContact() {
    this.nav.push(Contact);
  }
  toAbout() {
    this.nav.push(About);
  }
}
